package com.yanyilu.jianjian.util;

import com.yanyilu.jianjian.jwt.Audience;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
public class ParseJwtUtil {

    private static Audience audience;

    @Autowired
    public void setAudience(Audience audience) {

        this.audience = audience;
    }
    public static Integer getUserId(){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        if(request.getHeader("token") != null && request.getHeader("token") != ""){
            final String token = request.getHeader("token");
            Integer userid = Integer.parseInt(JwtTokenUtil.getUserId(token,audience.getBase64Secret()));
            return userid;
        }else {
            //请求头中没有token字段
            return 0;
        }
    }

    public static String getOpenid(){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        if(request.getHeader("token") != null && request.getHeader("token") != ""){
            final String token = request.getHeader("token");
            String openid = JwtTokenUtil.getUsername(token,audience.getBase64Secret());
            return openid;
        }else {
            //请求头中没有token字段
            return null;
        }
    }

    /*
    * 根据token获取权限
    * */
    public static String getRole(Audience audience){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        if(request.getHeader("token") != null && request.getHeader("token") != ""){
            final String token = request.getHeader("token");
            //System.out.println(audience.getBase64Secret());
            String role = JwtTokenUtil.getRole(token,audience.getBase64Secret());
            //System.out.println(role);
            return role;
        }else {
            //请求头中没有token字段
            return null;
        }
    }

    public static String getRole(){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        if(request.getHeader("token") != null && request.getHeader("token") != ""){
            final String token = request.getHeader("token");
            //System.out.println(audience.getBase64Secret());
            String role = JwtTokenUtil.getRole(token,audience.getBase64Secret());
            //System.out.println(role);
            return role;
        }else {
            //请求头中没有token字段
            return null;
        }
    }

    public static String getUserName(Audience audience){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        if(request.getHeader("token") != null && request.getHeader("token") != ""){
            final String token = request.getHeader("token");
            //System.out.println(audience.getBase64Secret());
            String role = JwtTokenUtil.getUsername(token,audience.getBase64Secret());
            //System.out.println(role);
            return role;
        }else {
            //请求头中没有token字段
            return null;
        }
    }
}
